package com.example.utils;

import com.example.common.BaseContext;
import org.springframework.util.AntPathMatcher;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 路径工具类
 *
 * @author czx
 * @since 2022/5/11 16:33
 */


public class PathUtils {
    /**
     * 路径匹配器
     */
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    /**
     * 路径匹配，检查本次请求是否需要放行
     *
     * @param urls       需要放行的请求
     * @param requestURI 页面请求
     * @return 判断结果
     */
    public static boolean check(String[] urls, String requestURI) {
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestURI);
            if (match) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否登陆
     * @param request 请求
     * @param response 响应
     * @param login 用户或员工
     * @return 是否登陆
     * @throws ServletException 服务异常
     * @throws IOException IO异常
     */
    public static boolean checkPass(HttpServletRequest request, HttpServletResponse response,String login) throws ServletException, IOException {
        // 5、判断用户登录状态，如果登录则直接放行
        if (request.getSession().getAttribute(login) != null) {
            //将id存进线程中
            Long id = (Long) request.getSession().getAttribute(login);
            BaseContext.setCurrentId(id);
            return true;
        }
        return false;
    }

}
